package com.ruoyi.operation.order.gift.controller;

import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.operation.order.gift.domain.Gift;
import com.ruoyi.operation.order.gift.service.IGiftService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @Author king
 * @create 2019/4/10 16:37
 */
@RequestMapping(value = "/gift/gift")
@Controller
public class GiftController extends BaseController {

    // 定义一个跳转到页面的路径
    private String prefix = "order/gift/";

    @Autowired
    private IGiftService iGiftService;


    /**
     * 跳转到显示赠品信息的页面
     * @return
     */
    @RequiresPermissions(value = "gift:gift:gift")
    @GetMapping()
    String gift(){
        return prefix + "gift";
    }


    /**
     * 查询所有的赠品信息
     * @param gift
     * @return
     */
    @RequestMapping(value = "/list")
    @RequiresPermissions(value = "gift:gift:gift")
    @ResponseBody
    public TableDataInfo list(Gift gift){
        startPage();
        List<Gift> giftList = iGiftService.allGiftList(gift);
        return getDataTable(giftList);
    }


    /**
     * 跳转到新增赠品信息的页面
     * @return
     */
    @RequiresPermissions(value = "gift:gift:add")
    @RequestMapping(value = "/add")
    String add(){
        return prefix + "add";
    }

    /**
     * 新增赠品信息
     * @param gift
     * @return
     */
    @RequestMapping(value = "/addGift")
    @RequiresPermissions(value = "gift:gift:add")
    @ResponseBody
    public AjaxResult addGift(Gift gift){
        return  toAjax(iGiftService.addGift(gift));
    }


    /**
     * 跳转到编辑的页面
     * @param id
     * @param model
     * @param gift
     * @return
     */
    @RequestMapping(value = "/edit/{id}")
    @RequiresPermissions(value = "gift:gift:edit")
    String edit(@PathVariable("id") String id, Model model,Gift gift){
        if (null != id || id != ""){
            gift = iGiftService.getGiftById(id);
        }
        model.addAttribute("gift",gift);
        return prefix + "edit";
    }


    /**
     * 编辑赠品信息
     * @param gift
     * @return
     */
    @RequestMapping(value = "/edit")
    @RequiresPermissions(value = "gift:gift:edit")
    @ResponseBody
    public AjaxResult editGift(Gift gift){
        return toAjax(iGiftService.editGift(gift));
    }

    /**
     * 删除赠品信息
     * @param id
     * @return
     */
    @RequestMapping(value = "/remove/{id}")
    @RequiresPermissions(value = "gift:gift:remove")
    @ResponseBody
    public AjaxResult delGift(@PathVariable("id") String id){
        return toAjax(iGiftService.delGift(id));
    }





}
